package com.zyz.forum_test.util;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class ScreenshotUtil {
    public static void screenshot(WebDriver driver, String moduleName) {
        try {
            File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            // 按日期划分文件夹
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmssSSS");
            String data = dateFormat.format(System.currentTimeMillis());
            String time = timeFormat.format(System.currentTimeMillis());
            String imagePath = "screenshots/" + data + "/" + moduleName + "/" + time + ".png";
            FileUtils.copyFile(screenshotFile, new File(imagePath));
        } catch ( IOException exception) {
            exception.printStackTrace();
        }
    }
}
